package edu.mercer.sse554.bkp;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.google.common.base.Optional;
import com.google.common.collect.ImmutableCollection;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;

/**
 * SnappyCompressionResults collection report data for Compression task run 
 * in separate thread.<p>
 *
 */

public class SnappyCompressionResults implements 
		TaskResults<ImmutableMap<String, String>> 
{
	private Map<String,String> _info = new HashMap<>();
	
	private Optional<SnappyCRBuilder> _builder = Optional.absent();
	
	
	SnappyCompressionResults( final SnappyCRBuilder builder )
	{
		super();

		_builder = Optional.of( builder );
		
		init();
		
	}//constructor

	
	private SnappyCompressionResults init()
	{
		_info.put( "skipped", _builder.get()
									  .getTask()
									  .skipped()
									  .toString() );
		
		_info.put( "file-in",  _builder.get()
									   .getTask()
									   .fileUncompressed() );

		String fileOutIfAny = "";
		
		if( !_builder.get()
					 .getTask()
					 .skipped() )
		{
			fileOutIfAny = _builder.get()
								   .getTask()
			  			   	   	   .fileCompressed();
		}//if
		
		_info.put( "file-out", fileOutIfAny );

		_info.put( "abend",    _builder.get()
									   .getTask()
									   .abended()
									   .toString() );

		_info.put( "reduction",  _builder.get()
										 .getTask()
										 .shrinkage()
										 .toString() );
		
		_info.put( "validation", _builder.get()
										 .getTask()
										 .validated()
										 .toString() );

		_info.put( "seconds", String.valueOf( _builder.get()
									  .getTask()
									  .elapsed()) );
		
		return this;
		
	}//method
	
		
	@Override
	public ImmutableMap<String,String> info() 
	{		
		return ImmutableMap.copyOf(_info); 
	
	}//method

	
	@Override
	public ImmutableCollection<String> output() 
	{
		final Set<String> details = new HashSet<>();
		
		for( String element : _info.keySet() )
		{
			details.add(element + " : " + _info.get(element) );
			
		}//for
		
		return ImmutableSet.copyOf(details);
		
	}//method

	
	@Override
	public TaskResults<ImmutableMap<String, String>> print()
	{
		List<String> sortedOutput = new ArrayList<>();
		
		sortedOutput.addAll(output());
		
		Collections.sort( sortedOutput );
		
		final Iterator<String> record = sortedOutput.iterator();
		
		while( record.hasNext() )
		{
			System.out.println( record.next() );
			
		}//while
		
		return this;
		
	}//method
	
	
}//class


/**
 * SnappyCompressionResults Builder.<p>
 *
 */

class SnappyCRBuilder
{
	private Optional<SnappyCompression> _task = Optional.absent();
	
	
	private SnappyCRBuilder()
	{
		super();
		
	}//constructor
	
	
	static SnappyCRBuilder factory()
	{ 
		return new SnappyCRBuilder(); 
		
	}//method
	
	
	SnappyCRBuilder setTask( final SnappyCompression task )
	{
		_task = Optional.of( task );
		
		return this;
		
	}//method
	

	SnappyCompression getTask()
	{
		return _task.get();
		
	}//method

	
	SnappyCompressionResults build()
	{
		if( !_task.isPresent() )
		{ 
			throw new IllegalStateException("SnappyCompression task not" +
					" supplied to Snappy Results Builder");
		
		}//if
		
		return new SnappyCompressionResults( this );
		
	}//method
	
	
}//class